<?php

namespace app\admin\controller;

use app\admin\model\PohtoMenuModel;
use app\admin\service\OssService;
use app\admin\validate\CommonnValidate;
use app\admin\validate\PhotoValidate;
use app\BaseController;
use app\admin\model\PohtoList as PhotoListModal;
use app\admin\model\PohtoListImage;

class PhotoMenu extends BaseController
{
    protected $middleware = ['Auth'];
    public function allList()
    {
        $params = $this->request->get(['photo_id']);
        $sql = new PohtoMenuModel();
        $whereArr[] = ["photo_id", "=", $params['photo_id']];
        $list = $sql->where($whereArr)->order('sort asc')
            ->order('create_at asc')->select();
        return result()->success($list);
    }
    public function list() {
        $params = $this->request->post(['current', 'size', 'photo_id']);
        $validate = new CommonnValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoMenuModel();
        $whereArr[] = ["photo_id", "=", $params['photo_id']];
        $list = $sql
            ->where($whereArr)
            ->order('sort asc')
            ->order('create_at asc')
            ->paginate([
                'list_rows'=> request()->param('size'),
                'page' => request()->param('current'),
            ])->toArray();
        return result()->success($list);
    }

    public function addEdit() {
        $params = $this->request->post(['id']);
        if(isset($params['id']) && $params['id']) {
            return $this->edit();
        }
        return $this->add();
    }

    public function add() {
        $params = $this->request->post(['photo_id', 'name', 'remark', 'sort']);
        $validate = new PhotoValidate();
        if (!$validate->scene('addMenu')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoMenuModel();
        $sql->save($params);
        return result()->success($sql->id);
    }

    public function edit() {
        $params = $this->request->post(['id', 'photo_id', 'name', 'remark', 'sort']);
        $validate = new CommonnValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoMenuModel();
        $info = $sql->find($params['id']);
        $info->save($params);
        return result()->success("修改成功");
    }

    public function dels() {
        $params = $this->request->post(['ids']);
        $validate = new CommonnValidate();
        if (!$validate->scene('dels')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoMenuModel();
        $sql->destroy($params['ids']);
        return result()->success("删除成功");
    }

    public function getImg()
    {
        $params = $this->request->post(['menu_id']);
        $imgSql = new PohtoListImage();
        $whereArr[] = ["menu_id", "=", $params['menu_id']];
        $list = $imgSql
            ->where($whereArr)
            ->order('sort asc')
            ->order('create_at asc')
            ->select();
        return result()->success($list);
    }
}